package org.dandsoft.blackbox.utils.list;

import junit.framework.TestCase;

import java.util.ArrayList;

/**
 * @author Vilyuzhanin Denis (Fin)
 */
public class ListElementTest extends TestCase {

    public void testAddToRight() {
        final int COUNT = 5;
        ArrayList<Integer> samples = new ArrayList<Integer>(COUNT);
        ArrayList<ListElement> elements = new ArrayList<ListElement>(COUNT);
        for (int i = 0; i < COUNT; i++) {
            samples.add(i);
            elements.add(new ListElement<Integer>(i));
        }

        for (int i = 0; i < elements.size() - 2; i += 2) {
            ListElement<Integer> e1 = elements.get(i);
            ListElement<Integer> e2 = elements.get(i + 2);
            e1.connectOnRight(e2);
        }

        ListElementIterator<Integer> iterator = new ListElementIterator<Integer>(elements.get(0));
        int previous = -2;
        while (iterator.hasNextRight()) {
            ListElement<Integer> element = iterator.nextRight();
            assertEquals(element.getElement() - 2, previous);
            previous = element.getElement();
        }

        for (int i = 1; i < elements.size(); i += 2) {
            ListElement<Integer> e1 = elements.get(i - 1);
            ListElement<Integer> e2 = elements.get(i);
            e1.connectOnRight(e2);
        }

        iterator = new ListElementIterator<Integer>(elements.get(0));
        previous = -1;
        while (iterator.hasNextRight()) {
            ListElement<Integer> element = iterator.nextRight();
            assertEquals(element.getElement() - 1, previous);
            previous = element.getElement();
        }


    }

    public void testAddToLeft() {
        final int COUNT = 5;
        ArrayList<Integer> samples = new ArrayList<Integer>(COUNT);
        ArrayList<ListElement> elements = new ArrayList<ListElement>(COUNT);
        for (int i = 0; i < COUNT; i++) {
            samples.add(i);
            elements.add(new ListElement<Integer>(i));
        }

        for (int i = 0; i < elements.size() - 2; i += 2) {
            ListElement<Integer> e1 = elements.get(i);
            ListElement<Integer> e2 = elements.get(i + 2);
            e2.connectOnLeft(e1);
        }

        ListElementIterator<Integer> iterator = new ListElementIterator<Integer>(elements.get(0));
        int previous = -2;
        while (iterator.hasNextRight()) {
            ListElement<Integer> element = iterator.nextRight();
            assertEquals(element.getElement() - 2, previous);
            previous = element.getElement();
        }

        for (int i = 1; i < elements.size(); i += 2) {
            ListElement<Integer> e1 = elements.get(i - 1);
            ListElement<Integer> e2 = elements.get(i);

            e2.connectOnLeft(e1);
        }

        iterator = new ListElementIterator<Integer>(elements.get(0));
        previous = -1;
        while (iterator.hasNextRight()) {
            ListElement<Integer> element = iterator.nextRight();
            assertEquals(element.getElement() - 1, previous);
            previous = element.getElement();
        }


    }
}
